var pageSize = 10;
var pageNum = 1;
var page;
var laypage;
var list;
var layer;


$(function () {
    $("#menuId").val(sessionStorage.menuId);
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate','element','laypage','layer'], function(){
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入



        var start = {
            min: laydate.now()
            ,max: '2099-06-16 23:59:59'
            ,istoday: true
            ,choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            min: laydate.now()
            ,max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };


    });
    setTimeout(function () {
        findAgentConfingList();
    },200);

});



//查询商铺列表
function findAgentConfingList(){
    var menuId = $("#menuId").val();
    var configName = $("#configName").val();
  
    var params = {
        "type":"query",
        "menuId":menuId,
        "pageSize":pageSize,
        "pageNum":pageNum,
        "configName":configName
    };
    $.ajax({
       type:"post",
       url: "/agentConfig/findAgentConfingList",
       data:params,
       dataType: "json",
       success : function(data){
            if(data.code == 1 && data.code != undefined){
                list = data.agentConfigList;
                page = data.page;
                var html = "";
                for(var b in list){
                    var state;

                    html += '<tr>' +
                        '<td>'+list[b].configKey+'</td>' +
                        '<td>'+list[b].configName+'</td>' +
                        '<td>'+(list[b].configContent == null ? "" : list[b].configContent)+'</td>' +
                        '<td>'+list[b].configDesc+'</td>' +
                        '<td><button class="layui-btn layui-btn-normal" onclick="findDetails('+b+')">修改</button></td></tr>'
                }
                $("#bus").html(html);
                $("#total").html("共有数据："+page.total+" 条");
                laypage({
                    cont: 'page'
                    ,pages: page.pages
                    ,first: 1
                    ,curr:pageNum
                    ,last: page.pages
                    ,prev: '<em><</em>'
                    ,next: '<em>></em>'
                    ,jump: function (obj,first) {
                        pageNum = obj.curr;
                        if(!first){
                            findAgentConfingList();
                        }
                    }
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }
       }

    });
}
//条件查询
function ck(obj){
    pageNum = 1;
    findAgentConfingList();
}

//查询按钮
function search() {
    pageNum = 1;
    findAgentConfingList();
}

//查看详情
function findDetails(b){
    var mc = list[b]; //配置信息
    var orderHtml = 
    	 '<div class="layui-form-item">'+
    	 '<label class="layui-form-label">配置key:</label>'+
    	 	'<div class="layui-input-inline">'+
    	 		'<label class="layui-form-label">'+mc.configKey+'</label>'+
    	 	'</div>'+
    	 '</div>'+
        '<div class="layui-form-item">'+
        	'<label class="layui-form-label">配置名称:</label>'+
        	'<div class="layui-input-inline">'+
        		'<input id="upconfigName" type="text" value="'+mc.configName+'" class="layui-input" style="width:300px;" ></input>'+
        		'<input id="configId" type="hidden" value="'+mc.id+'" class="layui-input" style="width:300px;" ></input>'+
        	'</div>'+
        '</div>'+
        '<div class="layui-form-item">'+
	    	'<label class="layui-form-label">配置内容:</label>'+
	    	'<div class="layui-input-inline">'+
	    		'<input id="upconfigContent" type="text" value="'+mc.configContent+'" class="layui-input" style="width:300px;"></input>'+
	    	'</div>'+
    	'</div>'+
    	'<div class="layui-form-item">'+
	   	 	'<label class="layui-form-label">配置描述:</label>'+
	   	 	'<div class="layui-input-inline">'+
	   	 		'<label class="layui-form-label" style="width:500px; text-align: left;">'+mc.configDesc+'</label>'+
	   	 	'</div>'+
   	 	'</div>'+
   	 	'<div class="layui-form-item">'+
   	    '<div class="layui-input-block">'+
   	    '  <button class="layui-btn" align="center" onclick="saveConfig()">立即修改</button>'+
   	    '<button class="layui-btn layui-btn-normal" onclick="closePop()">关闭</button>'+
   	    '</div>'+
   	  '</div>'
       

    var menuId = $("#menuId").val();
    var params = {
        "type":"query",
        "menuId":menuId,
        "userId":list[b].userId
    };
    
    orderHtml += '';
    layer.open({
        title: ['配置详情','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: orderHtml
        ,area: ['38%', '60%']
        ,maxmin:true
    });
}

//保存配置信息
function saveConfig() {
	var menuId = $("#menuId").val();
	var configId = $("#configId").val();
	var upconfigName = $("#upconfigName").val();
	var upconfigContent = $("#upconfigContent").val();
	
	if(!isnull(upconfigName)){
		return false;
	}
	
	if(!validate(upconfigContent)){
		return false;
	}
	
	var params={
		"type":"change",
		"menuId":menuId,
		"configId":configId,
        "configName":upconfigName,
        "configContent":upconfigContent
	}
	
  $.ajax({
       type:"post",
       url: "/agentConfig/updateAgentConfigById",
       data:params,
       dataType: "json",
       success : function(data){
    	   if (data.code == 1 && data.code != undefined) {
               layer.msg(data.message, {icon: 6});
               layer.msg(data.message, {
            	   icon: 6,
            	   time: 1000 //1秒关闭（如果不配置，默认是3秒）
            	 }, function(){
            		 layer.closeAll('page');
            	 });
               findAgentConfingList();
           } else if (data == 9999 && data != undefined) {
               window.location.href = "/login_toLogin";
           } else if (data == 1111 && data != undefined) {
               alert("无权操作");
           } else if (data.code ==0 && data.code != undefined) {
               layer.msg(data.message, {
            	   icon: 5,
            	   time: 1000 //1秒关闭（如果不配置，默认是3秒）
            	 }, function(){
            		 layer.closeAll('page');
            	 });  
               findAgentConfingList();
           }
       }
    });
	
}

function validate(c){
var reg = new RegExp("^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
var reg1 = new RegExp("^[0-9]*$");
	
if(c =='' || c ==undefined){
	layer.msg("请输入配置内容", {icon: 5})
	return false;
}else if(!reg.test(c) && !reg1.test(c)){
	layer.msg("配置内容,请输入数字", {icon: 5})
	return false;
}else {
     return true;
 }
}

function isnull(v){
	if(v =='' || v ==undefined) {
		layer.msg("请输入配置名称", {icon: 5})
		return false;
	}else {
		return true;
	}
}

function closePop() {
	layer.closeAll('page');
}